package com.inossem.utils.scan;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.inossem.utils.R;
import com.inossem.utils.log.LogUtils;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;


public class ScanActivity extends Activity {

    public static final int REQUEST_CODE_SCAN = 1000;

    public static final String KEY_SCAN_PARAMETER = "key_scan_parameter";

    public static final String KEY_SCAN_RESULT = "key_scan_result";

    private static final boolean FLASH_LIGHT_OFF = false;

    private static final boolean FLASH_LIGHT_ON = true;

    private static final boolean CAMERA_FACING_BACK = false;

    private static final boolean CAMERA_FACING_FRONT = true;

    private static final boolean SWITCH_SCAN_TYPE_QRCODE = false;

    private static final boolean SWITCH_SCAN_TYPE_BARCODE = true;

    private static final int VIBRATE_TIME = 200;

    private boolean flashLightFlag = false;

    private boolean cameraFacingFlag = false;

    private boolean switchScanTypeFlag = false;

    private boolean isShowAblum = true;

    private boolean isShowFlashLight = true;

    private boolean isShowCameraFacing = true;

    private boolean isShowSwitchScanType = true;

    private QRCodeView scanView;

    private ImageView back, ablum, flashLight, cameraFacing, switchScanType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.utils_activity_scan);
        getExtra(ScanActivity.this);
        initView(ScanActivity.this);
        initData(ScanActivity.this);
    }

    private void getExtra(Activity activity) {
        ScanParameter scanParameter = (ScanParameter) activity.getIntent().getSerializableExtra(KEY_SCAN_PARAMETER);
        if (scanParameter == null) {
            scanParameter = new ScanParameter();
        }
        isShowAblum = scanParameter.isShowAblum();
        isShowFlashLight = scanParameter.isShowFlashLight();
        isShowCameraFacing = scanParameter.isShowCameraFacing();
        isShowSwitchScanType = scanParameter.isShowSwitchScanType();
    }

    private void initView(Activity activity) {
        scanView = (ZXingView) activity.findViewById(R.id.scanView);
        back = (ImageView) activity.findViewById(R.id.back);
        ablum = (ImageView) activity.findViewById(R.id.ablum);
        flashLight = (ImageView) activity.findViewById(R.id.flashLight);
        cameraFacing = (ImageView) activity.findViewById(R.id.cameraFacing);
        switchScanType = (ImageView) activity.findViewById(R.id.switchScanType);
    }

    private void initData(final Activity activity) {
        scanView.setDelegate(new QRCodeView.Delegate() {
            @Override
            public void onScanQRCodeSuccess(String result) {
                LogUtils.i("result:" + result);
                vibrate(activity);
                Intent intent = new Intent();
                intent.putExtra(KEY_SCAN_RESULT, result);
                setResult(Activity.RESULT_OK, intent);
                ScanActivity.this.finish();
            }

            @Override
            public void onScanQRCodeOpenCameraError() {
                Toast.makeText(activity, "open camera error !", Toast.LENGTH_LONG).show();
                ScanActivity.this.finish();
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScanActivity.this.finish();
            }
        });

        ablum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(activity, "choose picture", Toast.LENGTH_LONG).show();
            }
        });

        flashLight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (flashLightFlag) {
                    scanView.closeFlashlight();
                    flashLightFlag = FLASH_LIGHT_OFF;
                } else {
                    scanView.openFlashlight();
                    flashLightFlag = FLASH_LIGHT_ON;
                }
            }
        });

        cameraFacing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                scanView.stopCamera();
                if (cameraFacingFlag) {
                    scanView.startCamera(Camera.CameraInfo.CAMERA_FACING_BACK);
                    cameraFacingFlag = CAMERA_FACING_BACK;
                } else {
                    scanView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT);
                    cameraFacingFlag = CAMERA_FACING_FRONT;
                }
                scanView.showScanRect();
                scanView.startSpot();
            }
        });

        switchScanType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (switchScanTypeFlag) {
                    scanView.changeToScanQRCodeStyle();
                    switchScanTypeFlag = SWITCH_SCAN_TYPE_QRCODE;
                } else {
                    scanView.changeToScanBarcodeStyle();
                    switchScanTypeFlag = SWITCH_SCAN_TYPE_BARCODE;
                }
            }
        });

        if (isShowAblum) {
            ablum.setVisibility(View.VISIBLE);
        } else {
            ablum.setVisibility(View.GONE);
        }

        if (isShowFlashLight) {
            flashLight.setVisibility(View.VISIBLE);
        } else {
            flashLight.setVisibility(View.GONE);
        }

        if (isShowCameraFacing) {
            cameraFacing.setVisibility(View.VISIBLE);
        } else {
            cameraFacing.setVisibility(View.GONE);
        }

        if (isShowSwitchScanType) {
            switchScanType.setVisibility(View.VISIBLE);
        } else {
            switchScanType.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        scanView.startCamera();
        scanView.showScanRect();
        scanView.startSpot();
    }

    @Override
    protected void onStop() {
        super.onStop();
        scanView.stopCamera();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        scanView.onDestroy();
    }

    private void vibrate(Context context) {
        Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(VIBRATE_TIME);
    }
}
